#include <iostream>
#include <sstream>
#include "thread.h"
#include "threadpool.h"
#include "test_fs_operator.h"
#include "async_fs_operator.h"

int main()
{
	DIR_ID dirID;
	dirID._id = 0;
	TestFSOperator testFS;
	ThreadPool threadPool;
	AsyncFSOperator<TestFSOperator, ThreadPool> fs(testFS, threadPool);

	fs.GetSubdirInfosAsync(dirID, [=] (HResult hr, std::vector<DIR_INFO>&& dirInfos) {
		std::ostringstream s;
		s << "GetSubdirInfosAsync return " << hr;
		if (S_OK == hr)
		{
			s << " with {";
            for (size_t idx = 0; idx < dirInfos.size(); idx ++)
            {
				s << dirInfos[idx]._name << ':' << dirInfos[idx]._dirID._parts._dirSN << ", " ;
            }
			s << '}';
		}
		s << '\n';
		std::cout << s.str();
	});

	fs.GetSubdirInfoAsync(dirID, "abc", [] (HResult hr, DIR_INFO&& dirInfo) {
		std::ostringstream s;
		s << "GetSubdirInfoAsync(abc) return " << hr;
		if (S_OK == hr)
			s << " with " << dirInfo._name << ':' << dirInfo._dirID._parts._dirSN;
		s << '\n';
		std::cout << s.str();
	});

	fs.GetSubdirInfoAsync(dirID, "xyz", [] (HResult hr, DIR_INFO&& dirInfo) {
		std::ostringstream s;
		s << "GetSubdirInfoAsync(xyz) return " << hr;
		if (S_OK == hr)
			s << " with " << dirInfo._name << ':' << dirInfo._dirID._parts._dirSN;
		s << '\n';
		std::cout << s.str();
	});

	while (threadPool.PendingJobCount() + threadPool.ThreadCount() > 0)
	{
		std::this_thread::yield();
	}
	return 0;
}
